package com.proxyadvertise;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Robot;
import java.awt.event.InputEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserListener;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserNavigationEvent;

public class SSOSina extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4436678012187094122L;

	protected static final String LS = System.getProperty("line.separator");

	private static final String loginUrl = "http://weibo.com/";

	private static boolean AUTO_CLIECK = true;

	private static boolean AUTO_COMMENT_CLIECK = false;

	private static JWebBrowser webBrowser = new JWebBrowser();

	private static Robot robot = null;
		
	public SSOSina() {
		super(new BorderLayout());
		
		try {
			robot = new Robot();
		} catch (AWTException e1) {
			e1.printStackTrace();
		}
		
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory.createTitledBorder("新浪微博自动登录"));

		webBrowser.setButtonBarVisible(true);
		webBrowser.setMenuBarVisible(true);
		webBrowser.setBarsVisible(true);
		webBrowser.setStatusBarVisible(true);

		webBrowser.navigate(loginUrl);

		JWebBrowser.clearSessionCookies();

		webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
			public void locationChanged(WebBrowserNavigationEvent e) {
				final String newResourceLocation = e.getNewResourceLocation();
				if (newResourceLocation.startsWith("http://weibo.com/u/")) {
					AUTO_COMMENT_CLIECK = true;
					e.consume();
					webBrowser
							.navigate("http://weibo.com/1401780831/zeSK0xMii");
				}
			}
		});

		WebBrowserListener listener = new WebBrowserAdapter() {
			public void loadingProgressChanged(WebBrowserEvent e) {

				if (e.getWebBrowser().getLoadingProgress() == 100
						&& AUTO_CLIECK) {

					String script = "";

					script += " document.getElementById('login_form_savestate').checked=false;"
							+ LS;
					script += "var username = document.getElementsByName( 'username' )[0];"
							+ LS;
					script += "var password = document.getElementsByName( 'password' )[0];"
							+ LS;

					script += " username.click();username.value = 'zhuxue_qun@hotmail.com';"
							+ LS;
					script += " password.value = '124417';" + LS;

					System.out.println(script);
					try {
						Thread.sleep(1 * 1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}

					e.getWebBrowser().executeJavascript(script);

					AUTO_CLIECK = false;
				
					robot.delay(2000);

					// 点击密码
					robot.mouseMove(874, 467); // 移动
					robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
					robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键

					// 点击登录
					robot.mouseMove(853, 535); // 移动
					robot.delay(1000);
					robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
					robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键

				}else if (e.getWebBrowser().getLoadingProgress() == 100
						&& AUTO_COMMENT_CLIECK ) {
					AUTO_COMMENT_CLIECK = false;
					System.out.println( "=================" );				
					
					robot.mouseMove(1260,572); // 移动

					for (int i = 0; i < 3; i++) {
						robot.delay(1000);
						robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
						robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
					}
					
					
					String script = "";

					script += " document.getElementsByTagName('textarea')[0].value='真的有那么好用吗？';";

					System.out.println(script);
					try {
						Thread.sleep(1 * 1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}

					e.getWebBrowser().executeJavascript(script);
					
					
					//开始评论
					// 点击登录
					robot.mouseMove(675,509); // 移动
					robot.delay(1000);
					robot.mousePress(InputEvent.BUTTON1_MASK); // 按下鼠标左键
					robot.mouseRelease(InputEvent.BUTTON1_MASK); // 释放鼠标左键
					
					
				}

			}
		};

		webBrowser.addWebBrowserListener(listener);
		webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
		add(webBrowserPanel, BorderLayout.CENTER);

	}

	public static void main(String[] args) {
		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("新浪微博");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane().add(new SSOSina(), BorderLayout.CENTER);

				frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
				frame.setLocationByPlatform(true);

				frame.setLocationByPlatform(true);
				frame.setVisible(true);
			}
		});
		NativeInterface.runEventPump();

	}

}
